﻿using System.Collections.Generic;
using System.Linq;

namespace SimpleTaskManager
{
    public class Project
    {
        public string Description;
        public int Id;
        public string Title;


        public Project()
        {
            Id = GetHashCode();
        }

        public static Project Load(int Id)
        {
            foreach (Project project in Storage.Projects)
            {
                if (project.Id == Id)
                {
                    return project;
                }
            }
            return null;
        }

        public static List<Project> List(string name)
        {
            List<Project> result = new List<Project>();
            foreach (Project project in Storage.Projects)
            {
                if (project.Title.Contains(name))
                {
                    result.Add(project);
                }


            }
            return result;
        }

        public static List<Project> List(User user)
        {
            List<Project> result = new List<Project>();
            foreach (Project project in Storage.Projects)
            {
                foreach (var task in Task.List(project))
                {
                    if (task.AssignTo == user)
                    {
                        result.Add(project);
                    }
                }
            }

            return result.Distinct().ToList();
        }

        public void Save()
        {
            if (!Storage.Projects.Contains(this))
            {
                Storage.Projects.Add(this);
            }
        }

        public void Delete()
        {
            if (Storage.Projects.Contains(this))
            {
                Storage.Projects.Remove(this);
            }
        }

        public override bool Equals(object obj)
        {
            var project = (Project) obj;
            return project.Id == this.Id;
        }
    }
}